﻿Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient

Public Class LocalidadesDB
    Public Shared Function GetLocalidadesDeProvincia(ByVal id_pais As Integer, ByVal id_provincia As Integer) As DataSet
        Dim da As New SqlDataAdapter
        Dim ds As New DataSet
        Dim cmd As New SqlCommand("spGetLocalidades", DB.Conexion)

        cmd.CommandType = CommandType.StoredProcedure
        If id_pais <> 0 Then
            cmd.Parameters.AddWithValue("@id_pais", id_pais)
        End If

        If id_provincia <> 0 Then
            cmd.Parameters.AddWithValue("@id_provincia", id_provincia)
        End If

        da.SelectCommand = cmd

        da.Fill(ds, "Localidades")
        Return ds
    End Function
    Public Shared Sub InsertarLocalidad(ByVal localidad As String, ByVal id_provincia As Integer)
        Dim cmd As New SqlCommand("spInsertLocalidad", DB.Conexion)
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.AddWithValue("@id_provincia", id_provincia)
        cmd.Parameters.AddWithValue("@localidad", localidad)
        cmd.Parameters.Add("@error", SqlDbType.Int)
        cmd.Parameters.Add("@errorDesc", SqlDbType.VarChar, 200)
        cmd.Connection.Open()
        cmd.ExecuteNonQuery()
        cmd.Connection.Close()

        If cmd.Parameters("@error").Value <> 0 Then
            Throw New ApplicationException(cmd.Parameters("@errorDesc").Value.ToString())
        End If
    End Sub

    Public Shared Sub EliminarLocalidad(ByVal id_localidad As Integer)
        Dim cmd As New SqlCommand("spDeleteLocalidad", DB.Conexion)
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.AddWithValue("@id_localidad", id_localidad)
        cmd.Connection.Open()
        cmd.ExecuteNonQuery()
        cmd.Connection.Close()
    End Sub

    Public Shared Sub ActualizarLocalidad(ByVal id_localidad As Integer, ByVal Localidad As String)
        Dim cmd As New SqlCommand("spUpdateLocalidad", DB.Conexion)

        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.AddWithValue("@id_provincia", id_localidad)
        cmd.Parameters.AddWithValue("@provincia", Localidad)
        cmd.Connection.Open()
        cmd.ExecuteNonQuery()
        cmd.Connection.Close()
    End Sub
End Class
